//
//  JfinalPlugin.swift
//  xtools
//
//  Created by symbol on 2018/5/29.
//  Copyright © 2018年 symbol. All rights reserved.
//

import Cocoa

class JfinalViewController: NSViewController {

    //界面加载完成后修改主面板信息
    override func viewDidAppear() {
        self.view.window?.title = "Jfinal项目快速创建"
        self.view.layer?.backgroundColor = NSColor.clear.cgColor
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

    }

    override var representedObject: Any? {
        didSet {
            // Update the view, if already loaded.
        }
    }

    //项目存储路径
    @IBOutlet weak var FilePath: NSTextField!
    //获取项目名称
    @IBOutlet weak var ProjectName: NSTextField!
    //主入口类名
    @IBOutlet weak var MainClass: NSTextField!
    //端口号
    @IBOutlet weak var prot: NSTextField!

    //是否使用默认的页面路径
    @IBOutlet weak var defPath: NSButton!
    //自定义页面路径地址
    @IBOutlet weak var basePath: NSTextField!

    //上传地址
    @IBOutlet weak var uploadPath: NSTextField!
    //下载地址
    @IBOutlet weak var downloadPath: NSTextField!
    //包名
    @IBOutlet weak var package: NSTextField!

    //数据库地址
    @IBOutlet weak var dbsql: NSTextField!
    //数据库端口
    @IBOutlet weak var dbPort: NSTextFieldCell!
    //数据库名
    @IBOutlet weak var dbname: NSTextFieldCell!
    //数据库用户名
    @IBOutlet weak var user: NSTextFieldCell!
    //数据库密码
    @IBOutlet weak var password: NSTextFieldCell!

    // 实例化类
    let obj = baseElement()

    //点击DevMode单选按钮事件
    @IBAction func devyes(_ sender: NSButton) {
        obj.devMode = sender.tag
    }

    //添加启动后需要执行的动作
    @IBAction func afterway(_ sender: NSButton) {
        obj.after = sender.state.rawValue
    }

    //添加启动后需要执行的动作
    @IBAction func beforeway(_ sender: NSButton) {
        obj.before = sender.state.rawValue
    }

    //是否添加main方法
    @IBAction func mianway(_ sender: NSButton) {
        obj.main = sender.state.rawValue
    }

    //是否使用jetty服务
    @IBAction func jetty(_ sender: NSButton) {
        obj.jetty = sender.state.rawValue
    }

    //json选择动作
    @IBAction func json(_ sender: NSButton) {
        obj.json = sender.state.rawValue
    }

    //是否使用default viewpath
    @IBAction func defPath(_ sender: NSButton) {
        obj.defPath = sender.tag
    }

    //页面引擎选择
    @IBAction func viewEngine(_ sender: NSButton) {
        obj.viewEngine = sender.state.rawValue
    }

    //数据库类型
    @IBAction func dbType(_ sender: NSButton) {
        obj.dbType = sender.tag
    }

    //数据库连接池选择
    @IBAction func dbPool(_ sender: NSButton) {
        obj.dbPool = sender.tag
    }

    //取消按钮
    @IBAction func cancel(_ sender: NSButton) {
        //关闭当前窗口
        self.view.window?.close()
    }

    //完成按钮
    @IBAction func finish(_ sender: NSButton) {
        //校验路径
        let filePath = FilePath.stringValue
        let isfile = checknull(str: filePath, msg: "卧槽，你™要上天啊，不填写项目路径怎么生成文件")
        if isfile {
            return
        }
        obj.filepath = filePath

        //校验项目名
        let projectname = ProjectName.stringValue
        let isproj = checknull(str: projectname, msg: "项目名字都木有，罢工罢工")
        if isproj {
            return
        }
        obj.projectName = projectname

        //校验主入口名
        let configClass = MainClass.stringValue
        let isconfig = checknull(str: configClass, msg: "不提供主类名字那是不可能生成的，这辈子都不可能生成")
        if isconfig {
            return
        }
        obj.mainclass = configClass

        //数据库名
        let dbName = dbname.stringValue
        let isdbname = checknull(str: dbName, msg: "心已累,连数据库名字都不提供,我也不想工作行不行")
        if isdbname {
            return
        }
        obj.dbname = dbName

        //数据库用户名
        let dbuser = user.stringValue
        let isuser = checknull(str: dbuser, msg: "数据库用户名未填写,还自动生成个毛线")
        if isuser {
            return
        }
        obj.user = dbuser

        //验证密码
        let dbpwd = password.stringValue
        let isdbpwd = checknull(str: dbpwd, msg: "噶闲,连密码都不给我,你特么的是在扯淡吗")
        if isdbpwd {
            return
        }
        obj.password = dbpwd


        //defpath 为1 表示使用默认地址 不为1表示不用默认地址
        if obj.defPath == 1 {
            obj.basePath = "/"
        } else {
            let baseviewPath = basePath.stringValue
            let isbasePath = checknull(str: baseviewPath, msg: "嘿,你™取消了默认地址却又不写默认的地址是几个意思")
            if isbasePath {
                return
            }
        }

        obj.uploadPath = uploadPath.stringValue
        obj.downloadPath = downloadPath.stringValue
        obj.package = package.stringValue
        obj.dbsql = dbsql.stringValue
        obj.prot = prot.stringValue
        obj.dbPort = dbPort.stringValue

        let isTrue = generateFile().crate(obj: obj)
        if isTrue {
            tjil(str: "项目创建成功，jar下载中。。。请等待")
        }
    }

    //校验空值,true 是空值 false 不是空值
    func checknull(str: String, msg: String) -> Bool {
        if str == "" {
            tjil(str: msg)
            return true
        } else {
            return false
        }
    }

    //弹窗操作
    func tjil(str: String) {
        let alert = NSAlert()
        //增加一个按钮
        alert.addButton(withTitle: "确定")
        //提示标题
        alert.messageText = "警告"
        //提示详细内容
        alert.informativeText = str
        //设置警告框风格
        alert.alertStyle = .critical
        alert.beginSheetModal(for: self.view.window!)
    }

}
